import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pet/app/routes/app_pages.dart';
import 'package:pet/app/storage/token.dart';

class RouteAuthMiddleware extends GetMiddleware {
  RouteAuthMiddleware();

  @override
  RouteSettings? redirect(String? route) {
    TokenStorage tokeController = Get.find<TokenStorage>();
    if (route == Routes.LOGIN) {
      /// 重定向到客户列表页面
      if (!tokeController.getToken().isEmpty) {
        return RouteSettings( name: Routes.CLIENTS, arguments: {'autoLogin': true});
      } else {
        if (!tokeController.getUsername()?.isEmpty && !tokeController.getPassword()?.isEmpty) {
           return RouteSettings( name: Routes.FINGERPRINT);
        }
      }
    } else {
      if (tokeController.getToken().isEmpty) {
        Future.delayed(
            const Duration(seconds: 1), () => Get.snackbar("提示", "请先登录APP"));
        return RouteSettings(name: Routes.LOGIN);
      }
    }
  }
}
